package com.htdc.web.service;

import com.htdc.web.bean.Bill;
import com.htdc.web.dao.BillDao;
import com.htdc.web.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@Service
public class BillService {
    @Autowired
    private BillDao billDao;

    //根据AID查历史缴费账单
    public List<Bill> showAllPaidByAid(String aid){
        return billDao.showAllPaidByAid(aid);
    }

    //根据合同号查询资源（房间信息）


    //缴费
    public boolean payment(List<String> bids,Date date){
        if (bids!=null){
            for (String bid:bids)
            billDao.payBill(bid,date);
            return true;
        }
        return false;
    }

    //查看未支付账单
    public List<Bill> showUnpaidBillsByAid(String aid){

        List<Bill> unpaidBills= billDao.showUnpaidBillsByAid(aid);

        return unpaidBills;
    }

    //查看所有异常（拖欠费用账单）
    public List<Bill> showAllArrearage(Date sqlDate){

        return  billDao.showAllArrearage(sqlDate);
    }

//    public boolean autoCreatBill(List<Bill> billList){
//        if (billList!=null) {
//            for (Bill bill : billList) {
//                System.err.println("该合同生成的所有的账单为：" + bill);
//                billDao.autoCreateBill(bill);
//                return true;
//            }
//        }
//        return false;
//    }

    //中止合同，修改账单
    public boolean updateBillByAid(String aid,Date date){
        if(aid!=null){
            billDao.delectBillByAid(aid,date);
            return true;
        }
        return false;
    }

    //每次生成合同的同时生成账单，返回一个账单预计的总预计租金。
    public double calculateBill (String aid,double price,double create,String cycle,long free,String start,
                                 String end){
        Date date1=Date.valueOf(start);
        Date date2=Date.valueOf(end);
        int days=0;
        int time=0;//账单总数
        int yearTime=0;//一年的账单总数
        int createTime=0;//增长的总次数
        double planRentMoney=0;
        List<Bill> billList=new ArrayList<>();
        try {
            days=DateUtils.getDiffDays(start,end);
            //根据days和周期判断需要生成多少账单
            if(cycle.equals("月")){
                time=days/30;
                yearTime=12;
            }else if(cycle.equals("季")){
                time=days/90;
                yearTime=4;
            }else if(cycle.equals("半年")){
                time=days/182;
                yearTime=2;
            }else{//cycle=年
                time=days/365;
                yearTime=1;
            }
            if(time%yearTime==0){
                createTime=time/yearTime-1;
            }else {
                createTime=time/yearTime;
            }
        }catch (Exception e){
            e.printStackTrace();
            System.err.println("time报错ing");
        }finally {

        }
        //通过日历类处理时间
        Calendar calendarStart=Calendar.getInstance();
        Calendar calendarEnd=Calendar.getInstance();
        calendarStart.setTime(date1);
        calendarEnd.setTime(date2);
        int startYear=calendarStart.get(Calendar.YEAR);
        int startMonth=calendarStart.get(Calendar.MONTH);
        int startDay=calendarStart.get(Calendar.DATE);
        for(int i=0;i<=createTime;i++){//从开始合同计算的按年循环。
            if(i==0){
                for(int j=0;j<time;j++){
                    if(j<free){
                        Bill bill = getNormalBill(aid,cycle,price, calendarStart, startYear, startMonth, startDay, j);
                        bill.setPaystate("已支付");
                        billList.add(bill);
                        planRentMoney+=bill.getPrice();
                        billDao.autoCreateBill(bill);
                    }else if (j>=free&&j<yearTime){//i>=free，开始需要付款
                        //终止年等于开始年 或 终止年大于开始年 终止月小于开始月
                        //小于等于一年，正常收费price
                        Bill bill = getNormalBill(aid,cycle,price,calendarStart, startYear, startMonth, startDay, j);
                        bill.setPaystate("未支付");
                        billList.add(bill);
                        planRentMoney+=bill.getPrice();
                        billDao.autoCreateBill(bill);
                    }
                }
            }else {
                //price=price*(1+create*i)
                for (int j=0;j<yearTime;j++){
                    Bill bill = getNormalBill(aid,cycle,price*(1+create*i),calendarStart, startYear+i, startMonth, startDay,j);
                    billList.add(bill);
                    planRentMoney+=bill.getPrice();
                    billDao.autoCreateBill(bill);
                    if(time==yearTime*createTime+j+1){
                        break;
                    }
                }
            }
        }
        return planRentMoney;
    }

    private Bill getFreeBill(String aid,String cycle, Calendar calendarStart, int startYear, int startMonth, int startDay, int i) {
        //生成账单，但账单免费，主要生成时间。free为2，则生成两次免费账单
        Bill bill=new Bill();
        bill.setStart(new Date(calendarStart.getTime().getTime()));
        if(cycle.equals("月")){
            calendarStart.set(startYear,startMonth+(i+1)*1,startDay);
        }else if (cycle.equals("季")){
            calendarStart.set(startYear,startMonth+(i+1)*3,startDay);
        }else if(cycle.equals("半年")){
            calendarStart.set(startYear,startMonth+(i+1)*6,startDay);
        }else {
            calendarStart.set(startYear,startMonth+(i+1)*12,startDay);
        }
        bill.setEnd(new Date(calendarStart.getTime().getTime()));//生成结束时间
        bill.setAid(aid);
        bill.setBid(""+bill.getAid()+calendarStart.getTime().toString());
        bill.setCycle(cycle);
        bill.setPrice(0);
        return bill;
    }

    private Bill getNormalBill(String aid,String cycle, double price,Calendar calendarStart, int startYear, int startMonth, int startDay, int i){
        System.err.println("开始收费啦");
        Bill bill=new Bill();
        bill.setStart(new Date(calendarStart.getTime().getTime()));
        if(cycle.equals("月")){
            calendarStart.set(startYear,startMonth+(i+1)*1,startDay);
        }else if (cycle.equals("季")){
            calendarStart.set(startYear,startMonth+(i+1)*3,startDay);
        }else if(cycle.equals("半年")){
            calendarStart.set(startYear,startMonth+(i+1)*6,startDay);
        }else {
            calendarStart.set(startYear,startMonth+(i+1)*12,startDay);
        }
        bill.setEnd(new Date(calendarStart.getTime().getTime()));//生成结束时间
        bill.setAid(aid);
        bill.setBid(""+bill.getAid()+calendarStart.getTime().toString());
        bill.setCycle(cycle);
        bill.setPrice(price);
        return bill;
    }
}
